package com.base.myproject.client.HR.attendance;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BaseTreeStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.busi.BO.TreeNodeModelData;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseGridPanel;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.TreePanelEvent;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.TabItem;
import com.extjs.gxt.ui.client.widget.TabPanel;
import com.extjs.gxt.ui.client.widget.VerticalPanel;

import com.extjs.gxt.ui.client.widget.form.CheckBox;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.DateTimePropertyEditor;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CellEditor;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.CheckColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnData;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckCascade;
import com.extjs.gxt.ui.client.widget.treepanel.TreePanel.CheckNodes;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;


/**
 * 考勤规则
 * 
 * @author
 * update cxghiu410@gmail.com
 */
public class Codex extends BaseJDBCPanel {
	TabPanel tabPanel = null;

	BaseTreeStore<TreeNodeModelData> treeWorkStore = null;
	// 创建树形结构的数据表
	BaseTreeStore<TreeNodeModelData> treeStore = null;

	Button seach_bt = new Button("查询");

	BaseComboBoxForm<BaseModelData> bc3 = new BaseComboBoxForm<BaseModelData>(
			"id", "name", "systeminfo", "", true);

	public Codex() {
		init();
	}

	public void init() {
		initWeigetx();
		registerMainStore(getHolidayStore());
	}

	public void complete() {
		super.complete();
	}
	
	TabItem tabHoliday;
	TabItem tabLeave;
	TabItem tabOverTime;
	TabItem attType;
	AttTypeSet att =new AttTypeSet();
	VerticalPanel vp;
	public void initWeigetx() {
		HorizontalPanel w=new HorizontalPanel();
		w.setSpacing(5);
		tabPanel = new TabPanel();
		Tools.setResize(tabPanel, 1.00, 1.00);
		tabPanel.setPlain(true);
		tabPanel.addListener(Events.Select, getListener());
		tabHoliday = new TabItem("法定节假日");
		tabHoliday.setStyleAttribute("padding", "3px");
		tabHoliday.add(getHolidayGrid());
		tabPanel.add(tabHoliday);

		tabOverTime = new TabItem("时间差设置");
		tabOverTime.add(createhr_timeDiffGrid());
		tabOverTime.setStyleAttribute("padding", "3px");
		tabPanel.add(tabOverTime);
		
		attType = new TabItem("考勤机类型设置");
		attType.add(att.createGrid());
		attType.setStyleAttribute("padding", "3px");
		tabPanel.add(attType);

		w.add(tabPanel);
		add(w);
	}

	/**
	 * holiday
	 */
	DateField b_date = new DateField();
	DateField e_date = new DateField();
	BaseGrid Holiday_grid = null;
	BaseStore<BusiObject> Holiday_store = null;

	BaseGrid getHolidayGrid() {
		if (Holiday_grid == null) {
			createHolidayGrid();
		}
		return Holiday_grid;
	}
	

	void createHolidayGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> list = new ArrayList<ColumnConfig>();
		list.add(cbs.getColumn());

		ColumnConfig c2 = new ColumnConfig("name", "名称", 120);
		TextField<String> t2 = new TextField<String>();
		c2.setEditor(new CellEditor(t2));
		list.add(c2);

		ColumnConfig c3 = new ColumnConfig("bgnDate", "开始时间", 120);
		b_date.setPropertyEditor(new DateTimePropertyEditor("yyyy-MM-dd"));
		//b_date.addListener(Events.Change, getListener());
		c3.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		c3.setEditor(new CellEditor(b_date));
		list.add(c3);

		ColumnConfig c4 = new ColumnConfig("endDate", "截止时间", 120);
		e_date.setPropertyEditor(new DateTimePropertyEditor("yyyy-MM-dd"));
		//e_date.addListener(Events.Change, getListener());
		c4.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		c4.setEditor(new CellEditor(e_date));
		list.add(c4);
		ColumnConfig c5 = new ColumnConfig("level", "补假倍数", 120);
		TextField<String> t5 = new TextField<String>();
		c5.setEditor(new CellEditor(t5));
		list.add(c5);
		/**ColumnConfig c6 = new ColumnConfig("available", "有效", 120);
		TextField<String> t6 = new TextField<String>();
		c6.setEditor(new CellEditor(t6));
		list.add(c6);**/

		ColumnModel cm = new ColumnModel(list);
		Holiday_grid = new BaseGrid(getHolidayStore(), cm, true, true);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		Holiday_grid.addListener(Events.AfterEdit, getListener());
		Holiday_grid.setTitle("假日信息");
		Holiday_grid.setSelectionModel(cbs);
		//Holiday_grid.setSize(851, 380);
		Tools.setResize(Holiday_grid, 1.00, 0.94);
	}

	BaseStore<BusiObject> getHolidayStore() {
		if (Holiday_store == null) {
			createHolidayStore();
		}
		return Holiday_store;
	}

	void createHolidayStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");

		type.addField("name");
		type.getField("name").setType(String.class);
		type.addField("bgnDate");
		type.getField("bgnDate").setType(Date.class);
		type.addField("endDate");
		type.getField("endDate").setType(Date.class);
		type.addField("level");
		type.getField("level").setType(Long.class);
		type.addField("available");
		type.getField("available").setType(Long.class);

		StoreInfo si = new StoreInfo();
		si.setTablename("hr_Attendance_holiday");
		si.setSql("select * from hr_Attendance_holiday");
		si.setKeyname(new String[] { "icode" });

		Holiday_store = new BaseStore<BusiObject>(type, si);
	}

	/******************************/
	/*
	 * hr_timeDiff
	 */
	BaseStore<BusiObject> hr_timeDiffstore = null;

	BaseStore<BusiObject> createhr_timeDiffStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("type");
		type.getField("type").setType(String.class);
		type.addField("number");
		type.getField("number").setType(Integer.class);
		type.addField("timeType");
		type.getField("timeType").setType(String.class);
		StoreInfo info = new StoreInfo();
		info.setTablename("hr_Attendance_timeDiff");
		info.setKeyname(new String[] { "icode" });
		info.setSql("select * from hr_Attendance_timeDiff");
		hr_timeDiffstore = new BaseStore<BusiObject>(type, info);
		return hr_timeDiffstore;
	}

	BaseGrid hr_timeDiffgrid = null;

	BaseGrid createhr_timeDiffGrid() {
		CheckBoxSelectionModel cbs = new CheckBoxSelectionModel();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());

		ColumnConfig C2 = new ColumnConfig("type", "允许时间差类型", 120);
		TextField<String> t2 = new TextField<String>();
		C2.setEditor(new CellEditor(t2));
		columns.add(C2);
		ColumnConfig C3 = new ColumnConfig("number", "数值", 80);
		TextField<String> t3 = new TextField<String>();
		C3.setEditor(new CellEditor(t3));
		columns.add(C3);
		ColumnConfig C4 = new ColumnConfig("timeType", "时间类型", 80);
		BaseComboBoxX bc4 = new BaseComboBoxX("name","dictionary", "where type='time_type'");
		C4.setEditor(bc4.getCellEditor());
		GridCellRenderer<ModelData>	renderer = new GridCellRenderer<ModelData>() {              
		      public String render(ModelData model, String property, ColumnData d, int rowIndex,
		          int colIndex, ListStore<ModelData> store, Grid<ModelData> grid) {             
		    	  if(model.get(property) !=null && model.get(property) instanceof ModelData)
		    	  {
		    		  return ((ModelData)model.get(property)).get("value");
		    	  }                                                                            
		    	  return model.get(property);                                                     
		      }                                                                                 
		    };  
		C4.setRenderer(renderer);
		columns.add(C4);

		ColumnModel cm = new ColumnModel(columns);
		hr_timeDiffgrid = new BaseGrid(createhr_timeDiffStore(), cm, true,
				true);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		hr_timeDiffgrid.setSelectionModel(cbs);
		//hr_timeDiffgrid.setSize(851, 380);
		Tools.setResize(hr_timeDiffgrid, 1.00, 0.94);
		return hr_timeDiffgrid;
	}

	/*************************/

	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}
		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {
							if(be.getSource().equals(tabPanel)) {
								if(tabPanel.getSelectedItem().equals(tabHoliday)) {
									registerMainStore(Holiday_store);
								} else if(tabPanel.getSelectedItem().equals(tabOverTime)) {
									registerMainStore(hr_timeDiffstore);
								} else {
									registerMainStore(att.getStore());
								}
							}
						}else if (be.getType() == Events.Change) {
							if(be.getSource().equals(b_date)){//e_date
								//check_date(1,b_date.getRawValue());
								//GWT.log(b_date.getRawValue());
							}
							if(be.getSource().equals(e_date)){//e_date
								//check_date(2,e_date.getRawValue());
								//GWT.log(e_date.getRawValue());
							}
						}else if (be.getType() == Events.AfterEdit) {
							GridEvent<BusiObject> ge = (GridEvent) be;
							int index = ge.getRowIndex();
							//col_index = ge.getColIndex();// 获得列的index 从第一个复选框=1
							// 开始
							if (ge.getProperty().equals("bgnDate")) {// 
								check_date(1,b_date.getRawValue(),index);
							}else if (ge.getProperty().equals("endDate")) {// 
								check_date(2,e_date.getRawValue(),index);
							}
						}	
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}
	/************************************************************************************/
	private void check_date(final int d,String date,final int index){
		String where="";
		if(d==1){
			where =" where bgndate='"+date+"' ";
		}else{
			where =" where enddate='"+date+"' ";
		}
		String sql="select bgndate,enddate from hr_Attendance_holiday "+where;
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService2 = GWT
		.create(com.base.myproject.client.tools.GreetingService.class);
greetingService2.getDataSet(sql, new AsyncCallback<DataSet>() {
	@Override
	public void onFailure(Throwable caught) {
	}
	@Override
	public void onSuccess(DataSet result) {
		if(result.getRowCount()>0){
			if(d==1){
				Holiday_grid.setValue(index, "bgnDate", "");
			}else{
				Holiday_grid.setValue(index, "endDate", "");
			}
			Info.display("", "该时间已经存在！请按刷新按钮后重新选择!");
			
		}
	}
});
	}
	
}

